package yunwo.cn.yzstation.view.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.ICarOrdview;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.BasicAdapter;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.OrderinfodetailsBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.presenter.CarOrderDetailsPersenter;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/10/14.
 */
public class CarOrderDetailsActivity extends MyBaseActivity implements ICarOrdview,View.OnClickListener{

    private ListView mMyListview;
    private String mId;
    private CarOrderDetailsPersenter mCarOrderDetailsPersenter;
    private CarOrderDetailsAdapter mCarOrderDetailsAdapter;
    List<OrderinfodetailsBean.DataBean>  mSelectedCollectedCars;
    List<OrderinfodetailsBean.DataBean> mDataBeanList;
    private TextView mSelectTv;
    private ImageView mSelectimg;
    private boolean selectAll = true;
    private LinearLayout mAllchoose;
    private TextView mBatcharrival;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mId = getIntent().getStringExtra("id");
        iniView();
        initData();

    }

    private void initData() {
        mCarOrderDetailsPersenter.getData(mId);
    }

    private void iniView() {
        mCarOrderDetailsPersenter = new CarOrderDetailsPersenter(this, this);
        mSelectedCollectedCars = new ArrayList<>();
        mMyListview = (ListView) findViewById(R.id.myListview);
        mSelectTv = (TextView) findViewById(R.id.selectTv);
        mSelectimg = (ImageView) findViewById(R.id.selectimg);
        mAllchoose = (LinearLayout) findViewById(R.id.allchoose);
        mBatcharrival = (TextView) findViewById(R.id.batcharrival);
        mCarOrderDetailsAdapter = new CarOrderDetailsAdapter(CarOrderDetailsActivity.this);
//        carOrderDetailsAdapter.refreshDataAndNotifyDataSetChanged(list);

        mMyListview.setAdapter(mCarOrderDetailsAdapter);
        mAllchoose.setOnClickListener(this);
        mBatcharrival.setOnClickListener(this);

    }

    @Override
    protected String settiltile() {
        return "订单信息";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_orderdetails;
    }

    @Override
    public void getgetDataSuccess(List<OrderinfodetailsBean.DataBean> dataBeanList) {
        this.mDataBeanList = dataBeanList;
        mCarOrderDetailsAdapter.refreshDataAndNotifyDataSetChanged(dataBeanList);
    }

    @Override
    public void submitSuccess() {
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.allchoose:
                if (mDataBeanList.size() > 0) {
                    if (selectAll) {
                        mSelectTv.setText("取消全选");
                        mSelectimg.setImageResource(R.drawable.btn_gou);
                        mSelectedCollectedCars.addAll(mDataBeanList);
                        selectAll = false;
                    }else {
                        mSelectTv.setText("全选");
                        mSelectimg.setImageResource(R.drawable.btn_quanxuan);
                        mSelectedCollectedCars.clear();
                        selectAll = true;
                    }
                }
                mCarOrderDetailsAdapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                break;
            case R.id.batcharrival:
                if (mSelectedCollectedCars.size() > 0) {
                    mCarOrderDetailsPersenter.submit(mSelectedCollectedCars);
                }else {
                    Toast.makeText(CarOrderDetailsActivity.this, "请选择运单", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;
        }
    }

    public class CarOrderDetailsAdapter extends BasicAdapter<OrderinfodetailsBean.DataBean>{
        public CarOrderDetailsAdapter(Context mctx) {
            super(mctx);
        }

        @Override
        protected int setItemLayout() {
            return R.layout.lvitem_order;
        }

        @Override
        protected void bindData(View convertView, final OrderinfodetailsBean.DataBean data, final int position) {
            LinearLayout linear = get(convertView, R.id.linear);
            TextView waybill = get(convertView, R.id.waybill);
            final ImageView img = get(convertView, R.id.img);
            TextView consignorNameandPhone = get(convertView, R.id.consignorNameandPhone);
            TextView consignorAddress = get(convertView, R.id.consignorAddress);
            TextView consigneeNameandPhone = get(convertView, R.id.consigneeNameandPhone);
            TextView consigneeAddress = get(convertView, R.id.consigneeAddress);
            final TextView manual = get(convertView, R.id.manual);



            waybill.setText("子运单：" + data.getSubTrackingNumber());
            if (StringUtil.isEmpty(data.getConsignorName()) && StringUtil.isEmpty(data.getConsignorPhone())) {
                consignorNameandPhone.setText("未填写发货人信息");
            } else {
                consignorNameandPhone.setText(data.getConsignorName() + "  " + data.getConsignorPhone());
            }

            consignorAddress.setText(data.getBeginOrganName());
            consigneeNameandPhone.setText(data.getConsigneeName() + "  " + data.getConsigneePhone());
            consigneeAddress.setText(data.getEndOrganName());

            if (mDataBeanList.size() > 0) {
                if (mSelectedCollectedCars.contains(mDataBeanList.get(position))) {
                    img.setImageResource(R.drawable.btn_gou);
                }else {
                    img.setImageResource(R.drawable.btn_quanxuan);
                }
                linear.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mSelectedCollectedCars.contains(mDataBeanList.get(position))) {
                            img.setImageResource(R.drawable.btn_gou);
                            mSelectedCollectedCars.add(mDataBeanList.get(position));
                        }else {
                            img.setImageResource(R.drawable.btn_quanxuan);
                            mSelectedCollectedCars.remove(mDataBeanList.get(position));
                        }
                        if (mSelectedCollectedCars.size() == mDataBeanList.size()) {
                            mSelectTv.setText("取消全选");
                            mSelectimg.setImageResource(R.drawable.btn_gou);
                        }else {
                            mSelectTv.setText("全选");
                            mSelectimg.setImageResource(R.drawable.btn_quanxuan);

                        }

                    }
                });


            }





            manual.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(mctx);
                    builder.setCancelable(false);
                    builder.setTitle("确定到货？");
                    builder.setNegativeButton("取消", null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("params", data);
                            HttpManager.hpptPostObj(MyConstants.confirmGoods, map).execute(new GLCallBack() {
                                @Override
                                public void onResponseT(String result, int code, String msg, int id) {
                                    if (code == 200) {
                                        Toast.makeText(CarOrderDetailsActivity.this, "到货成功", Toast.LENGTH_SHORT).show();
                                        datas.remove(data);
                                        notifyDataSetChanged();
                                    } else {
                                        Toast.makeText(CarOrderDetailsActivity.this, msg, Toast.LENGTH_SHORT).show();

                                    }
                                }

                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Toast.makeText(CarOrderDetailsActivity.this, "服务器异常", Toast.LENGTH_SHORT).show();

                                }
                            });

                        }
                    });
                    builder.show();
                }
            });

        }
    }
}
